<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class T_WMS_STOCK_DOC extends Model
{
    protected $resultSetType = 'collection';
    public $table='T_WMS_STOCK_DOC';
    public $connection='mysql';

    public function list1($where=[]){
        $list1=$this->alias('a')
            ->leftjoin('T_WMS_STOCK_ITEM b','a.DOC_NO=b.DOC_NO')//仓库单据物料料号列表
            ->leftjoin('T_PM_PROJECT_DETAIL c','b.ITEM_CODE=c.ITEM_CODE')
            ->leftjoin('T_PM_MO_BASE d','c.PROJECT_NO=d.PROJECT_NO')//领料单对应一个制令单
            ->where($where)
//            ->where('b.STATUS','in',[3,4])
            ->field('a.DOC_NO,a.DOC_STATUS,a.PLAN_RECEIVE_DATE,a.CLOSE_TIME,c.PROJECT_NO,d.MO_DOC,a.CREATE_TIME,b.STATUS')
            ->order('CREATE_TIME','desc')
            ->select();
    //    dd($list1);
        if($list1!=[]){
            $list1=$list1->toarray();
            foreach ($list1 as $k=>$v){
                    switch($v['DOC_STATUS']) {
                        case 1:
                            $list1[$k]['DOC_STATUS'] = '开立';
                            break;
                        case 2:
                            $list1[$k]['DOC_STATUS'] = '操作中';
                            break;
                        case 3:
                            $list1[$k]['DOC_STATUS'] = '关结';
                            break;
                        case 4:
                            $list1[$k]['DOC_STATUS'] = '强制关结';
                            break;
                    }
            }
        }
        return $list1;
    }

    public function list2($where=[]){
        $list=$this->alias('a')
            ->leftjoin('T_WMS_STOCK_ITEM b','a.DOC_NO=b.DOC_NO')//仓库单据物料料号列表
            ->where($where)
            ->field('a.DOC_NO,a.DOC_STATUS,a.PLAN_RECEIVE_DATE,a.CLOSE_TIME,b.CONNECT_DOC')
            ->select();
        if($list!=[]){
            $list=$list->toarray();
            foreach ($list as $k=>$v){
                    switch($v['DOC_STATUS']) {
                        case 1:
                            $list[$k]['DOC_STATUS'] = '开立';
                            break;
                        case 2:
                            $list[$k]['DOC_STATUS'] = '操作中';
                            break;
                        case 3:
                            $list[$k]['DOC_STATUS'] = '关结';
                            break;
                        case 4:
                            $list[$k]['DOC_STATUS'] = '强制关结';
                            break;
                    }
                    $list[$k]['PLAN_RECEIVE_DATE']=substr((string)$v['PLAN_RECEIVE_DATE'],0,16);
                    $list[$k]['CLOSE_TIME']=substr((string)$v['CLOSE_TIME'],0,16);
            }
        }
        return $list;
    }

    public function count($where=[]){
        $count=$this->where($where)->field('ID')->count();
        return $count;
    }
}
